/**
 * Created by XGJ on 2017/9/20.
 */



import {Component} from "@angular/core";
import {GlobalData} from "../../providers/GlobalData";
import {NativeServiceC} from "../../providers/NativeServiceC";
import {HttpService} from "../../providers/HttpService";
import {NavController} from "ionic-angular";

@Component({
    selector:'page-shopping',
    templateUrl:'shopping.html'
})

export class ShoppingPage{

    items = [];
    nextUrl:string = '';
    isRefresh:boolean = true;

    constructor(private globalData:GlobalData,
                private nativeService:NativeServiceC,
                private httpService:HttpService,
                public navCtrl:NavController
    ){
    }

    ionViewDidLoad(){
        let url = '/v2/items';
        this.getShops(url);
    }

    doInFinite(event){
        this.getShops(this.nextUrl,{},event);
    }

    private getShops(url,sender?,infiniteScroll?){
        sender = sender || {
                gender: 1,
                generation: 1,
                limit: 20,
                offset: 0
            }
        if (!this.isRefresh){
            this.globalData.showLoading = false;
        }

         this.httpService.get(url,sender).subscribe(res =>{
             this.nativeService.hideLoading();
             if(this.isRefresh) this.items.length=0;
             this.items = this.items.concat(res.items);
             this.nextUrl = res.paging.next_url;
             this.isRefresh = false;
             infiniteScroll && infiniteScroll.complete();
         })
    }


}